package com.oeaide.workflow.aprendizaje;
import java.util.ArrayList;
import java.util.List;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.oeaide.workflow.aprendizaje.beans.ActividadBean;
import com.oeaide.workflow.aprendizaje.beans.CalificacionBean;
import com.oeaide.workflow.aprendizaje.beans.ReservaBean;

public class EjecutarActividadesProcessManager
{
	 static JbpmConfiguration jbpmConfiguration = null;
	   static EjecutarActividadesProcessManager __instance = null;

	   private EjecutarActividadesProcessManager() 
	    {
	        startConfiguration();
	    }

	   public static EjecutarActividadesProcessManager getInstance() {
	        if (__instance == null) {
	            __instance = new EjecutarActividadesProcessManager();
	        }
	        return __instance;
	    }
	    

	    static void startConfiguration() {
	        jbpmConfiguration = JbpmConfiguration.parseXmlString(
	                "<jbpm-configuration>" +
	                "  <jbpm-context>" +
	                "    <service name='persistence' " +
	                "             factory='org.jbpm.persistence.db.DbPersistenceServiceFactory' />" +
	                "    <service name='tx' " +
	                "	            factory='org.jbpm.tx.TxServiceFactory' />" +
	                "  </jbpm-context>" +
	                "  <string name='resource.hibernate.cfg.xml' " +
	                "          value='hibernate.cfg.xml' />" +
	                "  <string name='resource.business.calendar' " +
	                "          value='org/jbpm/calendar/jbpm.business.calendar.properties' />" +
	                "  <string name='resource.default.modules' " +
	                "          value='org/jbpm/graph/def/jbpm.default.modules.properties' />" +
	                "  <string name='resource.converter' " +
	                "          value='org/jbpm/db/hibernate/jbpm.converter.properties' />" +
	                "  <string name='resource.action.types' " +
	                "          value='org/jbpm/graph/action/action.types.xml' />" +
	                "  <string name='resource.node.types' " +
	                "          value='org/jbpm/graph/node/node.types.xml' />" +
	                "  <string name='resource.varmapping' " +
	                "          value='org/jbpm/context/exe/jbpm.varmapping.xml' />" +
	                "</jbpm-configuration>");
	
	    }
	    
	public void RegistrarActividad(ActividadBean actividad)
	{
		 
		        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
		        try {
		            GraphSession graphSession = jbpmContext.getGraphSession();
		            ProcessDefinition processDefinition =
		                    graphSession.findLatestProcessDefinition("EjecucionActividades");
		            ProcessInstance processInstance = new ProcessInstance(processDefinition);

		            ContextInstance contextInstance = processInstance.getContextInstance();
		            contextInstance.setVariable("calificaciones", actividad.getCalificaciones());
		            contextInstance.setVariable("codigoDocente", actividad.getCodigoDocente());
		            contextInstance.setVariable("codigoTipoActividad", actividad.getCodigoTipoActividad());
		            contextInstance.setVariable("descripcion", actividad.getDescripcion());
		            contextInstance.setVariable("esAsistencial", actividad.getEspecialidad());
		            contextInstance.setVariable("especialidad", actividad.getEspecialidad());
		            contextInstance.setVariable("fechaFin", actividad.getFechaFin());
		            contextInstance.setVariable("fechaInicio", actividad.getFechaInicio());
		            contextInstance.setVariable("nombreActividad", actividad.getNombreActividad());
		            contextInstance.setVariable("reservas", actividad.getReservas());
		            contextInstance.setVariable("servicio", actividad.getServicio());
		            Token token = processInstance.getRootToken();
		            token.signal();
		            token.signal();
		            jbpmContext.save(processInstance);
		        } finally {
		            jbpmContext.close();
		        }
	}

	 public List<ActividadBean> ListarActividadesPendientes()
	 {
	        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	        List<ActividadBean> actividades = new ArrayList<ActividadBean>();
	        try {
	            GraphSession graphSession = jbpmContext.getGraphSession();
	            ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition("SolicitudActividad");
	            List processInstances = graphSession.findProcessInstances(processDefinition.getId());
	            for (int i = 0; i < processInstances.size(); i++) {
	                ProcessInstance processInstance = (ProcessInstance) processInstances.get(i);
	                ContextInstance contextInstance = processInstance.getContextInstance();
	                ActividadBean nuevo = new ActividadBean();
	                nuevo.setCalificaciones((List<CalificacionBean>)contextInstance.getVariable("calificaciones"));
	                nuevo.setEspecialidad(contextInstance.getVariable("especialidad").toString());
	                nuevo.setCodigoDocente(contextInstance.getVariable("codigoDocente").toString());
	                nuevo.setCodigoTipoActividad(contextInstance.getVariable("codigoTipoActividad").toString());
	                nuevo.setDescripcion(contextInstance.getVariable("descripcion").toString());	
	                nuevo.setEsAsistencial(Boolean.parseBoolean(contextInstance.getVariable("esAsistencial").toString()));
	                nuevo.setFechaFin(contextInstance.getVariable("fechaFin").toString());
	                nuevo.setFechaInicio(contextInstance.getVariable("fechaInicio").toString());
	                nuevo.setNombreActividad(contextInstance.getVariable("tipoPeriodo").toString());
	                nuevo.setReservas((List<ReservaBean>)contextInstance.getVariable("reservas"));
	                actividades.add(nuevo);
	            }
	        } finally {
	            jbpmContext.close();
	        }
	        return actividades;
	    }

	 
	 public void AsignarDocente(ActividadBean actividad) 
	 {
		  JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	        try {
	            GraphSession graphSession = jbpmContext.getGraphSession();
	            ProcessDefinition processDefinition =
	                    graphSession.findLatestProcessDefinition("EjecucionActividades");
	            ProcessInstance processInstance = new ProcessInstance(processDefinition);

	            ContextInstance contextInstance = processInstance.getContextInstance();
	            contextInstance.setVariable("codigoDocente", actividad.getCodigoDocente());
	            
	            Token token = processInstance.getRootToken();
	            token.signal();
	            token.signal();
	            jbpmContext.save(processInstance);
	        } finally {
	            jbpmContext.close();
	        }
		   
		   
	 }
	 
	 public void RegistrarFormatosCalificacion(ActividadBean actividad) 
	 {
		 JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	        try {
	            GraphSession graphSession = jbpmContext.getGraphSession();
	            ProcessDefinition processDefinition =
	                    graphSession.findLatestProcessDefinition("EjecucionActividades");
	            ProcessInstance processInstance = new ProcessInstance(processDefinition);

	            ContextInstance contextInstance = processInstance.getContextInstance();
	            contextInstance.setVariable("calificaciones", actividad.getCalificaciones());
	            
	            Token token = processInstance.getRootToken();
	            token.signal();
	            token.signal();
	            jbpmContext.save(processInstance);
	        } finally {
	            jbpmContext.close();
	        }
		   
	 }
	 public void ReservarAula(ActividadBean actividad) 
	 {
		   JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
		   
		   
	 }
	 public  List<TaskInstance> ListarTareasDisponiblesxActividad() 
	 {
		   JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
		   List<TaskInstance> tareasDisponibles;
		   try {
	            GraphSession graphSession = jbpmContext.getGraphSession();
	            ProcessDefinition processDefinition =
	                    graphSession.findLatestProcessDefinition("EjecucionActividades");
	            ProcessInstance processInstance = new ProcessInstance(processDefinition);
	            tareasDisponibles=jbpmContext.getTaskMgmtSession().findTaskInstancesByProcessInstance(processInstance);
	            TaskInstance instance = new TaskInstance();
	            
	            jbpmContext.save(processInstance);
	        } finally {
	            jbpmContext.close();
	        }
	        return tareasDisponibles;
		   
		   
	 }
	 

}
